package org.chengn.level_middle.context2661;

import java.util.HashMap;
import java.util.Map;

public class Context2661 {
    public int firstCompleteIndex(int[] arr, int[][] mat) {
        int m = mat.length, n = mat[0].length;
        int[] arrNum = new int[arr.length + 1];
        Map<Integer, Point> map = new HashMap<>();

        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                map.put(mat[i][j], new Point(i, j));
            }
        }


        int[] dp = new int[m];
        int[] dp2 = new int[n];


        for (int i = 0; i < arr.length; ++i) {
            Point point = map.get(arr[i]);
            dp[point.x]++;
            dp2[point.y]++;

            if (dp[point.x] == n || dp2[point.y] == m) {
                return i;
            }
        }
        return -1;

    }

    static class Point {
        public int x;
        public int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
}
